import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class _39_组合总和 {
    @Test
    public void test(){

    }
    private List<List<Integer>> result =new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,new ArrayList<>(),0,target,0);
        return result;
    }

    public void backtracking(int[] candidates,List<Integer> path,int pathNum,int target, int startIndex ){
        //回溯结束条件
        if (pathNum==target){
            result.add(new ArrayList<>(path));
        }
        for (int i = startIndex; i < candidates.length; i++) {
            if (pathNum+candidates[i]<=target){
                //将当前元素放到path中，
                path.add(candidates[i]);
                //然后遍历下一个元素
                backtracking(candidates,path,pathNum+candidates[i],target,i);
                path.remove(path.size()-1);
            }
        }
    }



}